package com.cloud.sample.consumer.core.http;

import com.cloud.sample.consumer.core.constant.ConsumerConstants;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.HttpStatus;

/**
 * @author gjy
 * @since 2021/7/1 0001
 */
public class ApiResponse<T> extends BaseResponse {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public ApiResponse(T data) {
        super("");
    }

    public ApiResponse(String message) {
        super(message);
    }

    public ApiResponse(Integer code, String message) {
        super(code, message);
    }

    public ApiResponse(Integer code, String message, Integer errorCode) {
        super(code, message, errorCode);
    }

    public ApiResponse(Integer code, String message, Integer errorCode, T data) {
        super(code, message, errorCode);
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean error() {
        return this.errorCode != null;
    }

    public static Builder build() {
        return new Builder();
    }

    @Override
    public String toString() {
        return "ApiResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", errorCode=" + errorCode +
                ", data=" + data +
                '}';
    }

    public static class Builder {
        private Integer code;
        private String message;
        private Integer errorCode;

        public Builder setCode(Integer code) {
            this.code = code;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setErrorCode(Integer errorCode) {
            this.errorCode = errorCode;
            return this;
        }

        public <T> ApiResponse<T> ok() {
            return ok(null);
        }

        public <T> ApiResponse<T> ok(T data) {
            this.code = HttpStatus.OK.value();
            this.message = ConsumerConstants.SUCCESS;
            return build(data);
        }

        public <T> ApiResponse<T> ok(String message, T data) {
            this.code = HttpStatus.OK.value();
            this.message = message;
            return build(data);
        }


        public <T> ApiResponse<T> fail() {
            return fail(ConsumerConstants.FAIL);
        }

        public <T> ApiResponse<T> fail(String message) {
            this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
            this.message = message;
            return build(null);
        }

        public <T> ApiResponse<T> fail(Integer errorCode, String message) {
            this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
            this.errorCode = errorCode;
            this.message = message;
            return build(null);
        }

        private <T> ApiResponse<T> build(T data) {
            return new ApiResponse<T>(this.code, this.message, this.errorCode, data);
        }


    }
}
